/*
*************************************************************************
*西瑞克斯研发管理系统
*版 本: 1.0   
*日 期：02/16/2012    
*作 者： 王浩 QQ：873987060   
*************************************************************************   
*/ 
package com.application.systemmanager.struts.action;

import java.lang.reflect.InvocationTargetException;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.log4j.Logger;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

import com.application.role.entity.Role;
import com.application.systemmanager.commons.Constants;
import com.application.systemmanager.entity.AllowancePut;
import com.application.systemmanager.entity.Notice;
import com.application.systemmanager.entity.SystemConfig;
import com.application.systemmanager.entity.SystemHelp;
import com.application.systemmanager.service.QuartzConfigService;
import com.application.systemmanager.service.SystemManagerService;
import com.application.systemmanager.struts.form.AllowanceForm;
import com.application.systemmanager.struts.form.NoticeForm;
import com.application.systemmanager.struts.form.ProcessForm;
import com.application.systemmanager.struts.form.SystemConfigForm;
import com.application.systemmanager.struts.form.SystemHelpForm;
import com.application.usertable.entity.Usertable;
import com.manager.commons.CommonUtil;
import com.manager.commons.DateUtil;
import com.manager.commons.page.PageBean;

/**
 * Copyrights 1997-2008 西安协同时光软件有限公司<br />
 * Description:NoticeAction<br />
 * Version:V0.1<br />
 * Author: Code Generator <br />
 * History: <br />
 * 03/08/2010 SyncBase Code Generator initial version.
 */
public class SystemManagerAction extends DispatchAction {

	/**
	 * Service层实例
	 */
	private SystemManagerService systemManagerService;

	private QuartzConfigService quartzConfigService;
	/**
	 * 日志
	 */
	private Logger log = Logger.getLogger(this.getClass());

	/**
	 * 实体对象
	 */
	private Notice notice;

	/**
	 * 采用partialList方式分页程序
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return ActionForward
	 * @throws Exception
	 */
	@SuppressWarnings("unchecked")
	public ActionForward getNoticePartialList(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		log.debug("searchNotice" + "开始");
		NoticeForm notice = (NoticeForm) form;

		String roleName = null;
		Usertable currentUser = CommonUtil.getCurrentUser(request);
		List list = currentUser.getRoleList();
		if (list != null && list.size() > 0) {
			roleName = ((Role) list.get(0)).getName();
		}
		request.setAttribute("RoleName", roleName);
		// 统计总记录数的sql语句
		int resultSize = 0;
		// 初始化pageBean供分页所用
		PageBean pageBean = null;
		// 取得当前页list数据
		List noticeList = null;
		resultSize = systemManagerService.getAllCountNotice(notice);
		pageBean = PageBean.getPageBean("notice", resultSize, request);
		noticeList = systemManagerService.searchNotice(notice, pageBean);
		request.setAttribute("noticeList", noticeList);

		log.debug("searchNotice" + "结束");

		return mapping.findForward(Constants.SUCCESS);
	}

	/**
	 * 采用partialList方式分页程序 用于帮助模块
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return ActionForward
	 * @throws Exception
	 */
	@SuppressWarnings("unchecked")
	public ActionForward getSystemHelpList(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		log.debug("getSystemHelpList" + "开始");
		SystemHelpForm help = (SystemHelpForm) form;

		// 统计总记录数的sql语句
		int resultSize = 0;
		// 初始化pageBean供分页所用
		PageBean pageBean = null;
		// 取得当前页list数据
		List noticeList = null;
		resultSize = systemManagerService.getAllCountHelp(help);
		pageBean = PageBean.getPageBean("notice", resultSize, request);
		noticeList = systemManagerService.searchHelp(help, pageBean);
		request.setAttribute("helpList", noticeList);
		request.setAttribute("help", help);
		log.debug("getSystemHelpList" + "结束");

		return mapping.findForward(Constants.SUCCESS);
	}

	public ActionForward getProcessList(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		log.debug("getProcessList" + "开始");
		ProcessForm processForm = (ProcessForm) form;

		// 统计总记录数的sql语句
		int resultSize = 0;
		// 初始化pageBean供分页所用
		PageBean pageBean = null;
		// 取得当前页list数据
		List processList = null;
		resultSize = systemManagerService.findProcessCount(processForm);
		pageBean = PageBean.getPageBean("processTab", resultSize, request);
		processList = systemManagerService.findProcessList(processForm, pageBean);
		request.setAttribute("processList", processList);
		request.setAttribute("orgSelect", CommonUtil.getOrgSelect());
		log.debug("getProcessList" + "结束");
		return mapping.findForward(Constants.SUCCESS);
	}

	/**
	 * 采用partialList方式分页程序
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return ActionForward
	 * @throws Exception
	 */
	@SuppressWarnings("unchecked")
	public ActionForward getNoticeList(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		log.debug("getNoticeList" + "开始");
		NoticeForm notice = (NoticeForm) form;
		// 统计总记录数的sql语句
		int resultSize = 0;
		// 初始化pageBean供分页所用
		PageBean pageBean = null;
		// 取得当前页list数据
		List noticeList = null;
		resultSize = systemManagerService.getAllCountNotice(notice);
		pageBean = PageBean.getPageBean("notice", resultSize, request);
		noticeList = systemManagerService.searchNotice(notice, pageBean);
		request.setAttribute("noticeList", noticeList);

		log.debug("getNoticeList" + "结束");

		return mapping.findForward(Constants.SUCCESS);
	}

	/**
	 * TODO 获取津贴列表
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@SuppressWarnings("unchecked")
	public ActionForward getAllowancePutList(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		log.debug("searchNotice" + "开始");
		AllowanceForm allowance = (AllowanceForm) form;
		// 统计总记录数的sql语句
		int resultSize = 0;
		// 初始化pageBean供分页所用
		PageBean pageBean = null;
		// 取得当前页list数据
		List allowanceList = null;
		resultSize = systemManagerService.getAllCountAllowance(allowance);
		pageBean = PageBean.getPageBean("allowance", resultSize, request);
		allowanceList = systemManagerService.searchAllowance(allowance);
		request.setAttribute("allowanceList", allowanceList);

		log.debug("searchNotice" + "结束");

		return mapping.findForward(Constants.SUCCESS);
	}

	public ActionForward toUpdateAllowancePut(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		log.debug("toUpdateAllowance");
		AllowanceForm allowance = (AllowanceForm) form;
		allowance = this.systemManagerService.getAllowance(String
				.valueOf(allowance.getId()));
		request.setAttribute(Constants.ALLOWANCE, allowance);
		return mapping.findForward(Constants.SUCCESS);
	}

	public ActionForward updateAllowancePut(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		log.debug("updateAllowance");
		AllowancePut allowance = this.getAllowance(form);
		this.systemManagerService.updateObject(allowance);
		return mapping.findForward(Constants.SUCCESS);
	}

	public ActionForward changePassword(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		log.debug("updateAllowance");
		// String oldPass=request.getParameter("oldPass");
		// String newPass=request.getParameter("newPass");
		// String state="";
		// LoginUser
		// curUser=(LoginUser)request.getSession().getAttribute(BaseConstants.USER);
		// if(oldPass!=null && !oldPass.equals("")){
		// oldPass=this.closePassword(oldPass);
		// boolean
		// istrue=systemManagerService.checkUserPassword(curUser.getUsername(),oldPass);
		// if(istrue){
		// systemManagerService.updateUserPassword(curUser.getUsername(),this.closePassword(newPass));
		// state="2";//1代表更新成功
		// }else{
		// state="1";//1代表当前旧密码错误
		// //out.print("<script>alert('\u65e7\u5bc6\u7801\u8f93\u5165\u9519\u8bef\uff0c\u8bf7\u91cd\u65b0\u8f93\u5165');</script>");
		// //return null;
		// }
		// }
		// request.setAttribute("state", state);
		return mapping.findForward("success");
	}

//	/** 给password加密 */
//	public String closePassword(String passWord) {
//		BASE64Encoder baseEncoder = new BASE64Encoder();// password加密
//		byte[] bpassword = passWord.getBytes();
//		String encrptKey = baseEncoder.encodeBuffer(bpassword);
//		return encrptKey;
//	}
//
//	/**
//	 * 获得解密后的口令
//	 * 
//	 * @param encryptPassWord
//	 *            已加密的口令
//	 * @return 已解密的口令
//	 * @throws OperateException
//	 *             当该方法中出现异常时抛出此例外
//	 */
//	public String openPassWord(String encryptPassWord) {
//		BASE64Decoder baseDecoder = new BASE64Decoder();
//		byte[] bpassword = new byte[1];
//		try {
//			bpassword = baseDecoder.decodeBuffer(encryptPassWord);
//		} catch (Exception e) {
//			e.getMessage();
//		}
//		String password = new String(bpassword);
//		return password;
//	}

	/**
	 * 到达新增页面
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return ActionForward
	 * @throws Exception
	 */
	public ActionForward toAddNotice(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		log.debug("toAddNotices");
		NoticeForm noticeForm = (NoticeForm) form;
		if (request.getSession().getAttribute("loginUser") != null) {
			Usertable currentUser = CommonUtil.getCurrentUser(request);
			noticeForm.setReportName(currentUser.getRname());
		}
		noticeForm.setNoticeType(Long.valueOf(1));
		noticeForm.setReportDate(DateUtil
				.dateToString(new Date(), "yyyy-MM-dd"));
		return mapping.findForward(Constants.SUCCESS);
	}

	/**
	 * 到达新增页面
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return ActionForward
	 * @throws Exception
	 */
	public ActionForward toAddHelp(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		log.debug("toAddHelp");
		SystemHelpForm systemHelpForm = (SystemHelpForm) form;
		// if(request.getSession().getAttribute("user")!=null){
		// LoginUser user=(LoginUser)request.getSession().getAttribute("user");
		// systemHelpForm.setHelpPerson(user.getUname());
		// }
		return mapping.findForward(Constants.SUCCESS);
	}

	/**
	 * 增加
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return ActionForward
	 * @throws Exception
	 */
	public ActionForward addHelp(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		log.debug("addNotices");
		SystemHelp systemHelp = this.getSystemHelp(form);
		this.systemManagerService.insertObject(systemHelp);
		return mapping.findForward(Constants.SUCCESS);
	}

	public ActionForward toUpdateHelp(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		log.debug("toUpdateHelp");

		SystemHelp systemHelp = this.getSystemHelp(form);
		systemHelp = this.systemManagerService.getSystemHelp(systemHelp);
		request.setAttribute(Constants.SYSTEMHELP, systemHelp);
		return mapping.findForward(Constants.SUCCESS);
	}

	public ActionForward updateHelp(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		log.debug("updateHelp");
		SystemHelp systemHelp = this.getSystemHelp(form);
		this.systemManagerService.updateObject(systemHelp);
		return mapping.findForward(Constants.SUCCESS);
	}

	public ActionForward deleteHelp(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		log.debug("deleteHelp");
		SystemHelpForm systemHelpForm = (SystemHelpForm) form;
		this.systemManagerService.deleteSystemHelp(systemHelpForm);
		return mapping.findForward(Constants.SUCCESS);
	}

	
	/**
	 * 增加
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return ActionForward
	 * @throws Exception
	 */
	public ActionForward addNotice(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		log.debug("addNotices");
		Notice notice = this.getNotice(form);
		this.systemManagerService.insertObject(notice);
		return mapping.findForward(Constants.SUCCESS);
	}

	/**
	 * 删除
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return ActionForward
	 * @throws Exception
	 */
	public ActionForward deleteNotice(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		log.debug("deleteNotices");
		NoticeForm noticeForm = (NoticeForm) form;
		this.systemManagerService.deleteNotices(noticeForm);
		return mapping.findForward(Constants.SUCCESS);
	}

	/**
	 * 到达更新页面
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return ActionForward
	 * @throws Exception
	 */
	public ActionForward toUpdateNotice(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		log.debug("toUpdateNotices");

		Notice notice = this.getNotice(form);
		notice = this.systemManagerService.getNotice(notice);
		request.setAttribute(Constants.NOTICE, notice);
		return mapping.findForward(Constants.SUCCESS);
	}

	/**
	 * 查看页面
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return ActionForward
	 * @throws Exception
	 */
	public ActionForward viewNotice(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		log.debug("viewNotice");

		Notice notice = this.getNotice(form);
		notice = this.systemManagerService.getNotice(notice);
		request.setAttribute(Constants.NOTICE, notice);
		return mapping.findForward(Constants.SUCCESS);
	}

	/**
	 * 帮助查看页面
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return ActionForward
	 * @throws Exception
	 */
	public ActionForward viewHelp(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		log.debug("viewHelp");
		SystemHelpForm helpForm = (SystemHelpForm) form;
		// SystemHelp help = this.getNotice(form);
		SystemHelp systemHelp = new SystemHelp();
		systemHelp.setId(helpForm.getId());
		systemHelp = this.systemManagerService.getHlep(systemHelp);
		request.setAttribute(Constants.HELP, systemHelp);
		return mapping.findForward(Constants.SUCCESS);
	}

	/**
	 * 更新
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return ActionForward
	 * @throws Exception
	 */
	public ActionForward updateNotice(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		log.debug("updateNotices");
		Notice notice = this.getNotice(form);
		this.systemManagerService.updateObject(notice);
		return mapping.findForward(Constants.SUCCESS);
	}

	public SystemManagerService getSystemManagerService() {
		return systemManagerService;
	}

	public void setSystemManagerService(
			SystemManagerService systemManagerService) {
		this.systemManagerService = systemManagerService;
	}

	/**
	 * 获取Notice实体
	 * 
	 * @param ActionForm
	 * @throws InvocationTargetException
	 * @throws IllegalAccessException
	 * @return the Notice
	 */
	public Notice getNotice(ActionForm form) throws IllegalAccessException,
			InvocationTargetException {
		NoticeForm noticeForm = (NoticeForm) form;
		notice = new Notice();
		BeanUtils.copyProperties(notice, noticeForm);
		return notice;
	}

	public SystemHelp getSystemHelp(ActionForm form)
			throws IllegalAccessException, InvocationTargetException {
		SystemHelpForm systemHelpForm = (SystemHelpForm) form;
		SystemHelp systemHelp = new SystemHelp();
		BeanUtils.copyProperties(systemHelp, systemHelpForm);
		return systemHelp;
	}

	public AllowancePut getAllowance(ActionForm form)
			throws IllegalAccessException, InvocationTargetException {
		AllowanceForm allowanceForm = (AllowanceForm) form;
		AllowancePut allowance = new AllowancePut();
		BeanUtils.copyProperties(allowance, allowanceForm);
		return allowance;
	}

	public void setNotice(Notice notice) {
		this.notice = notice;
	}

	/**
	 * 实体对象
	 */
	private SystemConfig systemConfig;

	/**
	 * 到达新增页面
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return ActionForward
	 * @throws Exception
	 */
	public ActionForward toAddSystemConfig(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		log.debug("toAddSystemConfigs");
		SystemConfigForm systemConfigForm = (SystemConfigForm) form;
		Map systemConfigMap = systemManagerService.searchSystemConfig();
		request.setAttribute("systemConfigMap", systemConfigMap);
		return mapping.findForward(Constants.SUCCESS);
	}

	/**
	 * 增加
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return ActionForward
	 * @throws Exception
	 */
	public ActionForward addSystemConfig(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		log.debug("addSystemConfigs");
		SystemConfigForm systemConfigForm = (SystemConfigForm) form;
		Map parmMap = request.getParameterMap();
		Iterator it = parmMap.entrySet().iterator();
		Map systemConfigMap = new HashMap();
		while (it.hasNext()) {
			Map.Entry entry = (Entry) it.next();
			String key = (String) entry.getKey();
			if (key.indexOf("*") < 0) {
				continue;
			}
			String value = ((String[]) entry.getValue())[0];
			String pre = key.substring(0, key.indexOf("*"));
			String property = key.substring(key.indexOf("*") + 1);
			if (systemConfigMap.containsKey(pre)) {
				SystemConfigForm conForm = (SystemConfigForm) systemConfigMap
						.get(pre);
				BeanUtils.setProperty(conForm, property, value);
			} else {
				systemConfigForm = new SystemConfigForm();
				BeanUtils.setProperty(systemConfigForm, property, value);
				systemConfigMap.put(pre, systemConfigForm);
			}

		}
		Iterator its = systemConfigMap.values().iterator();
		while (its.hasNext()) {
			SystemConfig sysConfig = this
					.getSystemConfig((SystemConfigForm) its.next());
			if (sysConfig.getCofigCode().equals("4100101")) {
				quartzConfigService.updateTrigger(sysConfig);
			}
			this.systemManagerService.updateObject(systemConfig);
		}
		return mapping.findForward(Constants.SUCCESS);
	}

	/**
	 * 更新
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return ActionForward
	 * @throws Exception
	 */
	public ActionForward updateSystemConfig(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		log.debug("updateSystemConfigs");
		SystemConfig systemConfig = this.getSystemConfig(form);
		this.systemManagerService.updateObject(systemConfig);

		return mapping.findForward(Constants.SUCCESS);
	}

	/**
	 * 获取SystemConfig实体
	 * 
	 * @param ActionForm
	 * @throws InvocationTargetException
	 * @throws IllegalAccessException
	 * @return the SystemConfig
	 */
	public SystemConfig getSystemConfig(ActionForm form)
			throws IllegalAccessException, InvocationTargetException {
		SystemConfigForm systemConfigForm = (SystemConfigForm) form;
		systemConfig = new SystemConfig();
		BeanUtils.copyProperties(systemConfig, systemConfigForm);
		return systemConfig;
	}

	public void setQuartzConfigService(QuartzConfigService quartzConfigService) {
		this.quartzConfigService = quartzConfigService;
	}

}
